package com.ytxg.health_management.comment.controller;

import com.ytxg.health_management.comment.service.CommentService;
import com.ytxg.health_management.controller.UserController;
import com.ytxg.health_management.pojo.dto.UserDto;
import com.ytxg.health_management.pojo.entity.Comments;
import com.ytxg.health_management.likeRecords.entity.LikeRecords;
import com.ytxg.health_management.pojo.entity.Result;
import com.ytxg.health_management.likeRecords.service.LikeRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;
    @Autowired
    UserController userController;
    @Autowired
    LikeRecordsService recordsService;



    @GetMapping("/getComment")
    public Result<List<Comments>> getComment(@RequestParam("articleId") Integer articleId) {

        return commentService.getCommentsByArticleId(articleId);
    }

    @PostMapping("/getCommentAll")
    public Result<Map<String, Object>> getComment(@RequestBody Comments comments) {
        return commentService.getCommentsAll(comments);
    }

    @PostMapping("/addComment")
    public Result<String> addComment(@RequestBody Comments comments, @RequestHeader("Authorization") String token) {

        Result<UserDto> userDtoResult = userController.tokenAuth(token);

        return commentService.addComment(comments);
    }

    @GetMapping("/deleteComment")
    public Result<String> deleteComment(@RequestParam("id") Integer id, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);

        return commentService.deleteComment(id);
    }

    @PostMapping("/addLike")
    public Result<String> addLike(@RequestBody LikeRecords likeRecords, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return recordsService.addLike(likeRecords,userDtoResult.getData());
    }

    /**
     * 回复评论
     */
    @PostMapping("/reply")
    public Result<String> reply(@RequestBody Comments comments, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        comments.setUserId(userDtoResult.getData().getId());
        return commentService.addComment(comments);
    }
}
